﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShildFunction : MonoBehaviour
{
    public float m_FixedEnargy=0;
    public Shild m_Shild;
    public GameObject m_ExplosionEffectPerfab;
    bool m_InitColor = false;
    void Start() 
    {
    }

    // Update is called once per frame
    void Update()
    {

    }
    private void FixedUpdate()
    {
        SpriteRenderer render = GetComponent<SpriteRenderer>();
        if (null == render)
            return;

        float enargyState = 0;
        if (m_Shild == null)
        {
            enargyState = 1;
        }
        else
        {
            if (m_InitColor == false)
            {
                if (InfluenceMG.get().isHostile(InfluenceMG.INFLUENCE_PLAYER, m_Shild.m_Influence))
                {
                    render.color = new Vector4(255f / 255.0f, 52.0f / 255.0f, 15.0f / 255.0f);
                }
                m_InitColor = true;
            }
            enargyState = Mathf.Clamp(m_Shild.Energy / m_Shild.m_EnergyMax + 0.3f, 0, 1f);
        }
        var color = render.color;
        color.a = enargyState;
        render.color = color;

    }
    public void Boom(float damage, Weapon.DamageType type)
    {
        switch (type)
        {
            case Weapon.DamageType.TYPE_EXPLOSION:
                damage /= 2;
                break;
            case Weapon.DamageType.TYPE_KINETIC:
                damage *= 2;
                break;
        }
        if(m_Shild != null)
        {
            m_Shild.Energy = Mathf.Max(0, m_Shild.Energy - damage);
            if(m_Shild.Energy == 0)
            {
                if (UtilsGame.IsOnline())
                {
                    m_Shild.ExplosionNet();
                }
                else
                {
                    m_Shild.Explosion();
                }
            }
        }
        else
        {
            m_FixedEnargy = Mathf.Max(0, m_FixedEnargy - damage);
            if(m_FixedEnargy <= 0 )
            {
                OnExplosion();
                gameObject.SetActive(false);
            }
        }
    }

    public void OnExplosion()
    {
        var particle = Instantiate<GameObject>(m_ExplosionEffectPerfab);
        if(particle)
        {
            particle.transform.SetParent(BulletPool.get().m_BulletPoolObj.transform);
            particle.transform.position = transform.position;
            var particleCom = particle.GetComponent<ParticleSystem>();
            particleCom.Play();
            Destroy(particle, 12);
        }
    }
}
